import { Component } from '@angular/core';
import { ToastController, NavController } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { StorageService } from '../../providers/StorageService';
import { CheckInService } from '../../providers/CheckInService';

import {RoomDetailPage} from "../roomDetail/roomDetail";

@Component({
  selector: 'page-setCheckIn',
  templateUrl: 'setCheckIn.html',
  providers: [CheckInService]
})
export class SetCheckInPage {
  checkInForm: FormGroup;
  user_info: any;
  user_id: number;
  room: any;
  room_id: number;
  room_name: string;
  build_address: string;
  currentFullYear: any;
  currentMonth: any;
  currentDate: any;
  current_date: any;

  constructor(
    private navCtrl: NavController,
    private formBuilder: FormBuilder,
    public toastCtrl: ToastController,
    private storageService: StorageService,
    private checkInService: CheckInService
  ){
    this.user_info = this.storageService.read('UserInfo');
    this.user_id = this.user_info.user_id;

    this.room = this.storageService.read('RoomInfo');
    this.room_id = this.room.room_id;
    this.room_name = this.room.room_name;
    this.build_address = this.room.build_address;

    this.checkInForm = this.formBuilder.group({
      'check_in_contact': ['', [Validators.required]],
      'check_in_mobile': ['', [Validators.required]],
      'check_in_date': ['', [Validators.required]],
      'check_in_deposit': ['', [Validators.required]],
      'check_in_rent': ['', [Validators.required]],
      'check_in_electricity': ['', [Validators.required]],
      'check_in_water': ['', [Validators.required]]
    });

    let date = new Date();
    this.currentFullYear = date.getFullYear();
    this.currentMonth = date.getMonth()+1;
    this.currentDate = date.getDate();

    if (this.currentMonth < 10) this.currentMonth = '0'+this.currentMonth;
    if (this.currentDate < 10) this.currentDate = '0'+this.currentDate;
    this.current_date = this.currentFullYear + '-' + this.currentMonth + '-' + this.currentDate;

    this.checkInForm.controls['check_in_date'].setValue(this.current_date);
  }

  save(checkIn, _event) {
    checkIn.user_id = this.user_info.user_id;
    checkIn.room_id = this.room_id;

    let error_message = '保存失败';

    _event.preventDefault();
    this.checkInService.save(checkIn).then(data => {
      if (data.status == 200)
      {
        this.navCtrl.push(RoomDetailPage, { room_name: this.room_name, room_id: this.room_id }).then(() => {
          const index = this.navCtrl.getActive().index;
          this.navCtrl.remove(index-1);
          this.navCtrl.remove(index-2);

          let toast = this.toastCtrl.create({
            message: '保存成功.',
            duration: 3000,
            position: 'top',
            showCloseButton: true,
            closeButtonText: '关闭'
          });
          toast.present();
        });
      }
      else
      {
        error_message = data.error.message;
        let toast = this.toastCtrl.create({
          message: error_message,
          duration: 3000,
          position: 'top',
          showCloseButton: true,
          closeButtonText: '关闭'
        });
        toast.present();
      }
    });
  }
}
